package com.allen.app.common.domain;

import io.swagger.annotations.ApiModelProperty;
import java.util.StringJoiner;

/**
 * @Classname R
 * @Description 统一返回
 * @Date 2021/09/09 08:49
 * @Created by Limiaojun
 */
@SuppressWarnings("unchecked")
public class R<T> {

    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private T data;

    private R() {
    }

    public static <T> R<T> ok() {
        R<T> r = new R<T>();
        r.setCode(200);
        r.setMessage("成功");
        return r;
    }

    public static <T> R<T> ok(T data) {
        R<T> r = new R<T>();
        r.setCode(200);
        r.setMessage("成功");
        r.setData(data);
        return r;
    }

    public static <T> R<T> error() {
        R<T> r = new R<T>();
        r.setCode(400);
        r.setMessage("失败");
        return r;
    }

    public static <T> R<T> error(String message) {
        R<T> r = new R<T>();
        r.setCode(400);
        r.setMessage(message);
        return r;
    }

    public static <T> R<T> error(Integer code,String message) {
        R<T> r = new R<T>();
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public Integer getCode() {
        return code;
    }

    public <T> R<T> setCode(Integer code) {
        this.code = code;
        return (R<T>) this;
    }

    public String getMessage() {
        return message;
    }

    public <T> R<T> setMessage(String message) {
        this.message = message;
        return (R<T>) this;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        StringJoiner sJoiner = new StringJoiner("", "R{", "}");
        sJoiner.add("code=").add(String.valueOf(code));
        sJoiner.add(", message=\"").add(message).add("\"");
        sJoiner.add(", data=").add(String.valueOf(data));
        return sJoiner.toString();
    }
}
